package Zoo.view.zoomanview;

import Zoo.entity.ZoomenEntity;
import Zoo.handler.ZoomenViewHandler;
import Zoo.impl.ZoomenServiceImpl;
import Zoo.service.ZoomenService;
import Zoo.util.TableDTO;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

/**
 * 动物饲养员登录后看到的园区信息界面
 */

public class ZoomenView extends JFrame {
    JPanel northPanel = new JPanel();
    JButton addBtn = new JButton("增加");
    JButton updateBtn = new JButton("修改");
    JButton delBtn = new JButton("删除");
    JTextField searchTxt = new JTextField(15);
    JButton searchBtn = new JButton("查询");
    ZoomenViewHandler zoomenViewHandler;
    private String zoomenName1;

    public ZoomenView(String zoomenName1) {

        super("动物园动物饲养员界面");
        Container contentPane = getContentPane();
        zoomenViewHandler = new ZoomenViewHandler(this);

        //放置北边的组件
        layoutNorth(contentPane);
        this.zoomenName1 = zoomenName1;


        Vector<Vector<Object>> data = getVectors();


        //tablemodel
        ZoomenTableModel zoomenTableModel = ZoomenTableModel.assembleModel(data);
        //jtable
        JTable jTable = new JTable(zoomenTableModel);
        //设置表头
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setFont(new Font(null, Font.BOLD, 16));
        tableHeader.setForeground(Color.RED);

        //设置表格体
        jTable.setFont(new Font(null, Font.PLAIN, 14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.BLACK);
        jTable.setRowHeight(30);
        //设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //设置表格列的渲染方式
        Vector<String> columns = zoomenTableModel.getColumns();
        ZoomenCellRender render = new ZoomenCellRender();
        for (int i = 0; i < columns.size(); i++) {
            TableColumn column = jTable.getColumn(columns.get(i));
            column.setCellRenderer(render);
        }


        JScrollPane jScrollPane = new JScrollPane(jTable);
        contentPane.add(jScrollPane);

        setSize(800, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //setResizable(false);
        setVisible(true);
    }

    //查询功能
    private Vector<Vector<Object>> getVectors() {
        ZoomenService zoomenService = new ZoomenServiceImpl();
        ZoomenEntity zoomenEntity = new ZoomenEntity();

        TableDTO tableDTO = zoomenService.retrieveZoomen(zoomenEntity, zoomenName1);
        Vector<Vector<Object>> data = tableDTO.getData();
        return data;
    }

    //放置北边组件
    private void layoutNorth(Container contentPane) {
        //增加事件监听
        addBtn.addActionListener(zoomenViewHandler);
        updateBtn.addActionListener(zoomenViewHandler);
        delBtn.addActionListener(zoomenViewHandler);
        searchBtn.addActionListener(zoomenViewHandler);

        northPanel.add(addBtn);
        northPanel.add(updateBtn);
        northPanel.add(delBtn);
        northPanel.add(searchTxt);
        northPanel.add(searchBtn);
        contentPane.add(northPanel, BorderLayout.NORTH);
    }


    //查询功能的实现
    public void reloadTable() {
        Vector<Vector<Object>> data = getVectors();
        ZoomenTableModel zoomenTableModel = new ZoomenTableModel();
        zoomenTableModel.updateModel(data);

    }


}

class ZoomenCellRender extends DefaultTableCellRenderer {
    //在每一行的每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (row % 2 == 0) {
            setBackground(Color.LIGHT_GRAY);
        } else {
            setBackground(Color.WHITE);
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

//自定义tablemodel
class ZoomenTableModel extends DefaultTableModel {
    static Vector<String> columns = new Vector<>();
    private static ZoomenTableModel zoomenTableModel = new ZoomenTableModel();

    static {
        columns.addElement("园区id");
        columns.addElement("园区类型");
        columns.addElement("简介");
        columns.addElement("占地面积");
        columns.addElement("地址");
        columns.addElement("开始时间");
        columns.addElement("结束时间");
        columns.addElement("园区管理员id");
    }

    public ZoomenTableModel() {
        super(null, columns);
    }

    public static ZoomenTableModel assembleModel(Vector<Vector<Object>> data) {
        zoomenTableModel.setDataVector(data, columns);
        return zoomenTableModel;
    }


    public static void updateModel(Vector<Vector<Object>> data) {
        zoomenTableModel.setDataVector(data, columns);
    }

    public static Vector<String> getColumns() {
        return columns;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }


}